import { createRouter, createWebHistory } from "vue-router";
import { useTabNar } from "@/stores/tabnar";
import { nextTick,markRaw  } from "vue";
import { useCommonStore } from "@/stores";
import {HelpFilled,Grid,HomeFilled,List} from '@element-plus/icons-vue';
// 避免深层响应
markRaw(HelpFilled);
markRaw(Grid);
markRaw(HomeFilled);
markRaw(List);
let tabNar = null, commonStore = null;
nextTick(() => {
  tabNar = useTabNar();
  commonStore = useCommonStore();
});

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/",
    },
    {
      path: "/user/login/:tk",
      name: 'login',
      meta:{
        isFull:true
      },  
      component: () => import("../views/user/login.vue"),
    },
    // 运营管理
    {
      path: "/operateManage",
      name: "operateManage",
      meta: {
        title: "运营管理",
        isSubMenu:true,
        icon: HelpFilled
      },
      component: () => import("../views/operateManage/index.vue"),
      redirect: "/operateManage/AfterSalesWorkOrder",
      children:[
        {
          path: "AfterSalesWorkOrder",
          name: "AfterSalesWorkOrder",
          meta: {
            title: "售后工单",
            keepAlive: true, // 需要被缓存
            isMenuItem:true
          },
          component: () => import("../views/operateManage/AfterSalesWorkOrder.vue"),
        },
        {
          path: "WorkOrderDetail",
          name: "WorkOrderDetail",
          meta: {
            title: "售后工单详情",
            keepAlive: true, // 需要被缓存
          },
          component: () => import("../views/operateManage/WorkOrderDetail.vue"),
        },
        {
          path: "VideoTutorial",
          name: "VideoTutorial",
          meta: {
            title: "视频教程",
            keepAlive: true, // 需要被缓存
            isMenuItem:true
          },
          component: () => import("../views/operateManage/VideoTutorial.vue"),
        },
        {
          path: "VideoTutorialAdd",
          name: "VideoTutorialAdd",
          meta: {
            title: "新增教程",
            KeepAlive: true, // 需要被缓存
          },
          component: () => import("../views/operateManage/VideoTutorialAdd.vue"),
        },
        {
          path: "AppNotifyConfig",
          name: "AppNotifyConfig",
          meta: {
            title: "APP通知",
            KeepAlive: true, // 需要被缓存
            isMenuItem:true
          },
          component: () => import("../views/operateManage/AppNotifyConfig.vue"),
        },
        {
          path: "AppNotifyConfigAdd",
          name: "AppNotifyConfigAdd",
          meta: {
            title: "新增APP通知消息",
            KeepAlive: true, // 需要被缓存
          },
          component: () => import("../views/operateManage/AppNotifyConfigAdd.vue"),
        },
      ]
    },
    // 活动管理
    {
      path: "/activityManage",
      name: "ActivityManage",
      meta: {
        title: "活动管理",
        keepAlive: true, // 需要被缓存
        isSubMenu:true,
        icon:Grid
      },
      component: () => import("../views/activityManage/index.vue"),
      redirect: "/activityManage/ActivityList",
      children:[
        {
          path: "ActivityList",
          name: "ActivityList",
          meta: {
            isMenuItem:true,
            title: "活动列表",
            keepAlive: true, // 需要被缓存
          },
          component: () => import("../views/activityManage/ActivityList.vue"),
        }
      ]
    },
    // 订单管理
    {
      path: "/orderManage",
      name: "OrderManage",
      meta: {
        title: "订单管理",
        keepAlive: true, // 需要被缓存
        isSubMenu:true,
        icon:List
      },
      component: () => import("../views/orderManage/index.vue"),
      redirect: "/orderManage/OrderList",
      children:[
        {
          path: "OrderList",
          name: "OrderList",
          meta: {
            title: "订单列表",
            keepAlive: true, // 需要被缓存
            isMenuItem:true,
          },
          component: () => import("../views/orderManage/OrderList.vue"),
        }
      ]
    },
    // 商家管理
    {
      path: "/merchantManage",
      name: "MerchantManage",
      meta: {
        title: "商家管理",
        keepAlive: true, // 需要被缓存
        isSubMenu:true,
        icon:HomeFilled
      },
      component: () => import("../views/merchantManage/index.vue"),
      redirect: "/merchantManage/MerchantList",
      children:[
        {
          path: "MerchantList",
          name: "MerchantList",
          meta: {
            title: "商家列表",
            keepAlive: true, // 需要被缓存
            isMenuItem:true,
          },
          component: () => import("../views/merchantManage/MerchantList.vue"),
        },
        {
          path: "MerchantAdd",
          name: "MerchantAdd",
          meta: {
            title: "新增国外商家",
            keepAlive: true, // 需要被缓存
          },
          component: () => import("../views/merchantManage/MerchantAdd.vue"),
        },
        {
          path: "MerchantDetail",
          name: "MerchantDetail",
          meta: {
            title: "商家详情",
            keepAlive: true, // 需要被缓存
          },
          component: () => import("../views/merchantManage/MerchantDetail.vue"),
        }
      ]
    },
    // 用户管理
    {
      path: "/userManage",
      name: "UserManage",
      meta: {
        title: "用户管理",
        keepAlive: true, // 需要被缓存
        isSubMenu:true,
        icon:HomeFilled
      },
      component: () => import("../views/userManage/index.vue"),
      redirect: "/userManage/UserList",
      children:[
        {
          path: "UserList",
          name: "UserList",
          meta: {
            title: "用户列表",
            keepAlive: true, // 需要被缓存
            isMenuItem:true,
          },
          component: () => import("../views/userManage/UserList.vue"),
        },
        {
          path: "UserDetail",
          name: "UserDetail",
          meta: {
            title: "用户详情",
            keepAlive: true, // 需要被缓存
          },
          component: () => import("../views/userManage/UserDetail.vue"),
        }
      ]
    },
    //商品管理
    {
      path: "/productManage",
      name: "ProductManage",
      meta: {
        title: "商品管理",
        keepAlive: true, // 需要被缓存
        isSubMenu:true,
        icon:HomeFilled
      },
      component: () => import("../views/productManage/index.vue"),
      redirect: "/productManage/ProductList",
      children:[
        {
          path: "ProductList",
          name: "ProductList",
          meta: {
            title: "商品列表",
            keepAlive: true, // 需要被缓存
            isMenuItem:true,
          },
          component: () => import("../views/productManage/ProductList.vue"),
        },
        {
          path: "ProductAdd",
          name: "ProductAdd",
          meta: {
            title: "新增教程",
            KeepAlive: true, // 需要被缓存
          },
          component: () => import("../views/productManage/ProductAdd.vue"),
        },
        {
          path: "ProductDetail",
          name: "ProductDetail",
          meta: {
            title: "商品详情",
            keepAlive: true, // 需要被缓存
          },
          component: () => import("../views/productManage/ProductDetail.vue"),
        }
      ]
    }
  ],
});

router.beforeEach(async (to, from, next) => {
  const token = commonStore.token || localStorage.getItem("token");
  if (!token && to.name != 'login') {//重新登录
    localStorage.setItem("isFull" , true)
    return next('/user/login/getToken')
  }
  const isFull = localStorage.getItem("isFull");
  to.meta.isFull = isFull?true:false;
  document.title = to.meta.title || "管理系统";
  tabNar.addTab(to);
  tabNar.setCurrentTab(to);
  next();
});

export default router;
